﻿using EasyERP_Model;
using System.Data;
using System.Text;

namespace EasyERP_DAL.Base
{
    /// <summary>
    /// 子代码数据访问类
    /// </summary>
    public class B_Minor_DAL
    {
        #region 查询子代码信息
        /// <summary>
        /// 查询子代码信息
        /// </summary>
        /// <param name="model">子代码实体对象</param>
        /// <returns>返回查询表</returns>
        public DataTable Get_B_Minor(B_Minor model)
        {
            //sql语句
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat($@"SELECT minor_cd, minor_nm, remark
                FROM dbo.b_minor
                WHERE major_cd = N'{model.major_cd}';");

            return DbHelperSQL.Query(sb.ToString()).Tables[0];
        }
        #endregion

        #region 添加子代码信息
        /// <summary>
        /// 添加子代码信息
        /// </summary>
        /// <param name="model">子代码实体对象</param>
        /// <returns>返回True：成功；返回False：失败</returns>
        public bool Insert_B_Minor(B_Minor model)
        {
            //sql语句
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat($@"
                INSERT INTO dbo.b_minor
                    (major_cd, minor_cd, minor_nm, remark, ins_user, ins_date, up_user, up_date)
                VALUES(N'{model.major_cd}', N'{model.minor_cd}', N'{model.minor_nm}', 
                    N'{model.remark}', N'{model.user_cd}', GETDATE(), N'{model.user_cd}', GETDATE());
            ");

            return DbHelperSQL.ExecuteSql(sb.ToString()) > 0;
        }
        #endregion

        #region 删除子代码信息
        /// <summary>
        /// 删除子代码信息
        /// </summary>
        /// <param name="model">子代码实体对象</param>
        /// <returns>返回True：成功；返回False：失败</returns>
        public bool Delete_B_Minor(B_Minor model)
        {
            //sql语句
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat($@"
                DELETE FROM dbo.b_minor
                WHERE major_cd = N'{model.major_cd}'
                AND minor_cd = N'{model.minor_cd}';
            ");

            return DbHelperSQL.ExecuteSql(sb.ToString()) > 0;
        }
        #endregion

        #region 修改子代码信息
        /// <summary>
        /// 修改子代码信息
        /// </summary>
        /// <param name="model">子代码实体对象</param>
        /// <returns>返回True：成功；返回False：失败</returns>
        public bool Update_B_Minor(B_Minor model)
        {
            //sql语句
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat($@"
                UPDATE dbo.b_minor SET
                    minor_nm = N'{model.minor_nm}',
                    remark = N'{model.remark}',
                    up_user = N'{model.user_cd}',
                    up_date = GETDATE()
                WHERE major_cd = N'{model.major_cd}' 
                AND minor_cd = N'{model.minor_cd}';
             ");

            return DbHelperSQL.ExecuteSql(sb.ToString()) > 0;
        }
        #endregion

        #region 查询数据是否存在
        /// <summary>
        /// 查询数据是否存在
        /// </summary>
        /// <param name="model">子代码实体对象</param>
        /// <returns>返回True：存在；返回False：不存在</returns>
        public bool Exists(B_Minor model)
        {
            //sql语句
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat($@"
                SELECT 1 FROM dbo.b_minor
                WHERE major_cd = N'{model.major_cd}'
                AND minor_cd = N'{model.minor_cd}';
            ");

            return DbHelperSQL.Exists(sb.ToString());
        }
        #endregion

        #region 根据主代码查询子代码
        /// <summary>
        /// 根据主代码查询子代码
        /// </summary>
        /// <param name="model">子代码实体类</param>
        /// <returns>查询的子代码信息</returns>
        public DataTable Get_Minor_nm(B_Minor model)
        {
            //sql语句
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat($@"
                SELECT minor_cd,
                       minor_nm
                FROM dbo.b_minor
                WHERE major_cd = N'{model.major_cd}';
            ");

            return DbHelperSQL.Query(sb.ToString()).Tables[0];
        }
        #endregion

        #region 查询子代码信息,用于参照窗体FrmPopMinor
        /// <summary>
        /// 查询子代码信息,用于参照窗体FrmPopMinor
        /// </summary>
        /// <param name="model">子代码实体对象</param>
        /// <returns>返回查询表</returns>
        public DataTable Get_B_Minor_Pop(B_Minor model)
        {
            //sql语句
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat($@"
                SELECT minor_cd,
                       minor_nm,
                       remark
                FROM dbo.b_minor
                WHERE major_cd = N'{model.major_cd}'
                      AND minor_cd LIKE N'%{model.minor_cd}%'
                      AND minor_nm LIKE N'%{model.minor_nm}%';
            ");

            return DbHelperSQL.Query(sb.ToString()).Tables[0];
        }
        #endregion
    }
}
